#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include "recod.h"

#ifndef ISBNBUFSIZE
#define ISBNBUFSIZE 15
#endif // ISBNBUFSIZE

#define RECODNUMBUFSIZE 20
#define USERNUMBUFSIZE 10
#define OUTDATEBUFSIZE 30
#define INDATEBUFSIZE 30

static void map_recod_info(recod* new_recod, char* number, char* book_num, char* user_num, char* outdate, char* indate)
{
    new_recod->number = strdup(number);
    new_recod->book_num = strdup(book_num);
    new_recod->user_num = strdup(user_num);
    new_recod->outdate = strdup(outdate);
    if(indate == NULL)
        new_recod->indate = NULL;
    else if(strcmp(indate, "N\\A") == 0)
        new_recod->indate = NULL;
    else
        new_recod->indate = strdup(indate);
}

static char* get_system_time()
{
    time_t rawtime;
    struct tm* timeinfo;
    time (&rawtime);
    timeinfo = localtime(&rawtime);
    char* result_date = asctime(timeinfo);
    result_date[strlen(result_date) - 1] = '\0';
    return result_date;
}

recod* create_recods_list(char* recods_db_path)
{
    FILE* recod_db = initialize_file(recods_db_path);
    if (recod_db == NULL)
        return NULL;
    recod* first_recod = NULL;
    recod* current_recod = NULL;
    char number_buf[RECODNUMBUFSIZE];
    char book_buf[ISBNBUFSIZE];
    char user_buf[USERNUMBUFSIZE];
    char outdate_buf[OUTDATEBUFSIZE];
    char indate_buf[INDATEBUFSIZE];
    while(feof(recod_db) == 0)
    {

        if(fscanf(recod_db, "%[0-9]$%[0-9]$%[0-9]$%[^$]$%[^\n]\n", &number_buf, &book_buf, &user_buf, &outdate_buf, &indate_buf) == 5)
        {
            if(first_recod == NULL)
            {
                first_recod = (recod*)malloc(sizeof(recod));
                first_recod->previous = NULL;
                first_recod->next = NULL;
                map_recod_info(first_recod, number_buf, book_buf, user_buf, outdate_buf, indate_buf);
                current_recod = first_recod;
            }
            else
            {
                recod* temp_recod = (recod*)malloc(sizeof(recod));
                temp_recod->previous = current_recod;
                temp_recod->next = NULL;
                map_recod_info(temp_recod, number_buf, book_buf, user_buf, outdate_buf, indate_buf);
                current_recod->next = temp_recod;
                current_recod = temp_recod;
            }
        }
        else
        {
            break;
        }
    }
    fclose(recod_db);
    return first_recod;
}

void free_recod(recod* target)
{
    free(target->number);
    free(target->book_num);
    free(target->user_num);
    free(target->outdate);
    if(target->indate != NULL)
        free(target->indate);
}

void show_recod(recod* recods_list)
{
    if(recods_list != NULL)
    {
        do
        {
            char* temp_recod_indate;
            if(recods_list->indate == NULL)
                temp_recod_indate = "N\\A";
            else
                temp_recod_indate = recods_list->indate;
            printf("--------------------\n编号：%s\n图书编号：%s\n读者编号：%s\n借出时间：%s\n归还时间：%s\n--------------------\n", recods_list->number, recods_list->book_num, recods_list->user_num, recods_list->outdate, temp_recod_indate);
        }
        while((recods_list = recods_list->next) != NULL);
    }
}

recod* create_recod(user* users_list, book* books_list)
{
    char number_buf[RECODNUMBUFSIZE];
    char book_buf[ISBNBUFSIZE];
    char user_buf[USERNUMBUFSIZE];
    printf("编号（输入 auto 自动填充）：");
    scanf("%s", &number_buf);
    fflush(stdin);
    if(strcmp(number_buf, "auto") == 0)
    {
        time_t rawtime;
        struct tm* timeinfo;
        time (&rawtime);
        timeinfo = localtime(&rawtime);
        sprintf(&number_buf, "%04d%02d%02d%02d%02d%02d0", timeinfo->tm_year, timeinfo->tm_mon, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
        printf("编号填充为：%s\n", number_buf);
    }
    if(strlen(number_buf) != 15)
    {
        printf("编号应为 15 位\n");
        return NULL;
    }
    printf("图书编号：");
    scanf("%s", &book_buf);
    fflush(stdin);
    if(search_book(books_list, book_buf) == NULL)
    {
        printf("无效的图书编号 %s\n", book_buf);
        return NULL;
    }
    printf("读者编号：");
    scanf("%s", &user_buf);
    fflush(stdin);
    if(search_user(users_list, user_buf) == NULL)
    {
        printf("无效的读者编号 %s\n", user_buf);
        return NULL;
    }
    if(book_borrow_book(books_list, book_buf) != 1)
    {
        printf("图书 %s 已借完\n", book_buf);
        return NULL;
    }
    if(user_borrow_book(users_list, user_buf) != 1)
    {
        printf("读者 %s 已借满\n", user_buf);
        book_return_book(books_list, book_buf);
        return NULL;
    }
    recod* new_recod = (recod*)malloc(sizeof(recod));
    new_recod->previous = NULL;
    new_recod->next = NULL;
    map_recod_info(new_recod, number_buf, book_buf, user_buf, get_system_time(), NULL);
    return new_recod;
}

recod* search_recod(recod* recods_list, char* number)
{
    if(recods_list != NULL)
    {
        recod* result = NULL;
        do
        {
            if(strcmp(recods_list->number, number) == 0)
            {
                result = recods_list;
                break;
            }
        }
        while((recods_list = recods_list->next) != NULL);
        return result;
    }
    else
        return NULL;
}

void return_book(recod* recods_list, book* books_list, user* users_list)
{
    printf("请输入借阅编号：");
    char number_buf[RECODNUMBUFSIZE];
    scanf("%s", &number_buf);
    fflush(stdin);
    recod* recod_to_return = search_recod(recods_list, number_buf);
    if(recod_to_return == NULL)
    {
        printf("未找到借阅记录 %s\n", number_buf);
        return;
    }
    if(recod_to_return->indate != NULL)
    {
        printf("编号为 %s 的记录已经归还\n", number_buf);
        return;
    }
    book_return_book(books_list, recod_to_return->book_num);
    user_return_book(users_list, recod_to_return->user_num);
    recod_to_return->indate = strdup(get_system_time());
    printf("已完成！\n");
}

recod* interface_borrow_book(user* target_user, book* books_list)
{
    if(target_user->rest == 0)
    {
        printf("你已达到借书数量上限，请将之前借书归还后再借\n");
        return NULL;
    }
    char book_buf[ISBNBUFSIZE];
    printf("图书ISBN：");
    scanf("%s", &book_buf);
    fflush(stdin);
    if(search_book(books_list, book_buf) == NULL)
    {
        printf("无效的图书ISBN %s\n", book_buf);
        return NULL;
    }
    if(book_borrow_book(books_list, book_buf) != 1)
    {
        printf("图书 %s 已借完\n", book_buf);
        return NULL;
    }
    target_user->rest -= 1;
    recod* new_recod = (recod*)malloc(sizeof(recod));
    new_recod->previous = NULL;
    new_recod->next = NULL;
    char number_buf[RECODNUMBUFSIZE];
    time_t rawtime;
    struct tm* timeinfo;
    time (&rawtime);
    timeinfo = localtime(&rawtime);
    sprintf(&number_buf, "%04d%02d%02d%02d%02d%02d0", timeinfo->tm_year, timeinfo->tm_mon, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
    map_recod_info(new_recod, number_buf, book_buf, target_user->number, get_system_time(), NULL);
    return new_recod;
}

void interface_return_book(user* target_user, recod* recods_list, book* books_list)
{
    char book_buf[ISBNBUFSIZE];
    printf("图书ISBN：");
    scanf("%s", &book_buf);
    fflush(stdin);
    if(recods_list != NULL)
    {
        recod* current_recod = recods_list;
        do
        {
            if(strcmp(current_recod->book_num, book_buf) == 0 && strcmp(current_recod->user_num, target_user->number) == 0 && current_recod->indate == NULL)
            {
                target_user->rest += 1;
                book_return_book(books_list, book_buf);
                current_recod->indate = strdup(get_system_time());
                printf("图书 %s 成功归还\n", book_buf);
                return;
            }
        }
        while((current_recod = current_recod->next) != NULL);
    }
    printf("未借图书 %s\n", book_buf);
}

void show_borrowed_books(user* target_user, recod* recods_list, book* books_list)
{
    if(recods_list != NULL)
    {
        recod* current_recod = recods_list;
        do
        {
            if(strcmp(current_recod->user_num, target_user->number) ==0)
            {
                book* temp_book = search_book(books_list, current_recod->book_num);
                printf("ISBN：%s\n", temp_book->isbn);
                printf("标题：%s\n", temp_book->title);
                printf("作者：%s\n", temp_book->author);
                printf("出版社：%s\n", temp_book->press);
                printf("借出时间：%s\n", current_recod->outdate);
                if(current_recod->indate == NULL)
                {
                    printf("状态：未归还\n");
                }
                else
                {
                    printf("状态：已归还\n");
                    printf("归还时间：%s\n", current_recod->indate);
                }
                printf("----------------------------------------\n");
            }
        }
        while((current_recod = current_recod->next) != NULL);
    }
}
